package com.white.loggerserver

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.addTextChangedListener
import com.flask.colorpicker.ColorPickerView
import com.flask.colorpicker.builder.ColorPickerDialogBuilder
import com.white.loggerserver.room.MyRoomDb
import com.white.loggerserver.room.StyleConfig
import kotlinx.android.synthetic.main.activity_settings.*
import org.jetbrains.anko.toast


class SettingsActivity : AppCompatActivity(), View.OnClickListener {

    companion object {
        fun active(act: Activity) {
            val it = Intent(act, SettingsActivity::class.java)
            act.startActivityForResult(it, 12)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)

        supportActionBar?.let {
            it.title = "设置"
            it.setDisplayHomeAsUpEnabled(true)
        }

        configData()

        tagFontColor.setOnClickListener(this)
        tagBgColor.setOnClickListener(this)
        modifyStyle.setOnClickListener(this)
    }

    private val datas = mutableListOf<StyleConfig>()
    private var currentItem : StyleConfig? = null
    private fun configData() {
        val ret = MyRoomDb.instance.styleConfigDao().getAllRecord()
        if(ret.isNullOrEmpty()){
            val sti = StyleConfig(null, "i", 12, Color.GREEN, Color.TRANSPARENT)
            sti.id = MyRoomDb.instance.styleConfigDao().addStyle(sti)

            val std = StyleConfig(null, "d", 12, Color.BLUE, Color.TRANSPARENT)
            std.id = MyRoomDb.instance.styleConfigDao().addStyle(std)

            val ste = StyleConfig(null, "e", 12, Color.RED, Color.TRANSPARENT)
            ste.id = MyRoomDb.instance.styleConfigDao().addStyle(ste)

            datas.add(sti)
            datas.add(std)
            datas.add(ste)
        }else{
            datas.addAll(ret)
        }
        level_spinner.config(0, {item->
            currentItem = datas[item.index]
            tagFontSize.setText(currentItem!!.fontSize.toString())
            tagFontColor.setBackgroundColor(currentItem!!.fontColor)
            tagBgColor.setBackgroundColor(currentItem!!.bgColor)
        }, datas)

        tagFontSize.addTextChangedListener(afterTextChanged = {
            currentItem?.fontSize = tagFontSize.text.toString().toInt()
        })
    }

    override fun onClick(v: View?) {

        when(v){
            tagFontColor->{
                ColorPickerDialogBuilder
                    .with(this)
                    .setTitle("选择字体颜色")
                    .initialColor(Color.WHITE)
                    .wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
                    .density(12)
                    .setPositiveButton("选择") { _, selectedColor, _ ->
                        tagFontColor.setBackgroundColor(selectedColor)
                        currentItem?.fontColor = selectedColor
                    }
                    .setNegativeButton("取消") { _, _ -> }
                    .build()
                    .show()
            }
            tagBgColor->{
                ColorPickerDialogBuilder
                    .with(this)
                    .setTitle("选择背景颜色")
                    .initialColor(Color.WHITE)
                    .wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
                    .density(12)
                    .setPositiveButton("选择") { _, selectedColor, _ ->
                        tagBgColor.setBackgroundColor(selectedColor)
                        currentItem?.bgColor = selectedColor
                    }
                    .setNegativeButton("取消") { _, _ -> }
                    .build()
                    .show()
            }
            modifyStyle->{
                currentItem?.let {
                    MyRoomDb.instance.styleConfigDao().updateStyle(it)
                    setResult(Activity.RESULT_OK)
                    finish()
                }
            }
        }
    }


    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            android.R.id.home->{
                finish()
            }
        }
        return true
    }
}